package flc.ast.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

public class DraggableRelativeLayout extends RelativeLayout {

    private int lastX;
    private int lastY;
    private boolean isDragging;
    private static final int CLICK_DRAG_TOLERANCE = 10; // 可以根据需要调整这个值

    public DraggableRelativeLayout(Context context) {
        super(context);
        init();
    }

    public DraggableRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public DraggableRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int action = event.getAction();
                switch (action) {
                    case MotionEvent.ACTION_DOWN:
                        lastX = (int) event.getRawX();
                        lastY = (int) event.getRawY();
                        isDragging = false;
                        break;
                    case MotionEvent.ACTION_MOVE:
                        int rawX = (int) event.getRawX();
                        int rawY = (int) event.getRawY();
                        int offsetX = rawX - lastX;
                        int offsetY = rawY - lastY;

                        if (Math.abs(offsetX) > CLICK_DRAG_TOLERANCE || Math.abs(offsetY) > CLICK_DRAG_TOLERANCE) {
                            isDragging = true;

                            int left = getLeft() + offsetX;
                            int top = getTop() + offsetY;
                            int right = getRight() + offsetX;
                            int bottom = getBottom() + offsetY;

                            // 防止布局超出屏幕
                            ViewGroup parent = (ViewGroup) getParent();
                            int parentWidth = parent.getWidth();
                            int parentHeight = parent.getHeight();

                            if (left < 0) {
                                left = 0;
                                right = left + getWidth();
                            }
                            if (right > parentWidth) {
                                right = parentWidth;
                                left = right - getWidth();
                            }
                            if (top < 0) {
                                top = 0;
                                bottom = top + getHeight();
                            }
                            if (bottom > parentHeight) {
                                bottom = parentHeight;
                                top = bottom - getHeight();
                            }

                            layout(left, top, right, bottom);

                            lastX = rawX;
                            lastY = rawY;
                        }
                        break;
                    case MotionEvent.ACTION_UP:
                        if (!isDragging) {
                            performClick();
                        }
                        break;
                }
                return true;
            }
        });
    }

    @Override
    public boolean performClick() {
        super.performClick();
        return true;
    }
}
